<!-- 自定义托盘菜单，覆盖原生的menu，原生的功能偏少 -->
<template>
  <div class="actions-box">
    <div class="custom-button" @click="handleStartFlash">图标闪烁</div>
    <div class="custom-button" @click="handleStopFlash">停止闪烁</div>
    <div class="custom-button" @click="handleOpenWechat">打开微信</div>
    <div class="custom-button" @click="handleSetting">设置</div>
    <div class="custom-button exit" @click="handleExit">退出</div>
  </div>
</template>

<script setup>
import { invoke } from '@tauri-apps/api/core';
import { useCreateWindow } from "@/hooks/useCreateWindow.js";
const { openWindow, setWindowVisibilityAndTaskbar, setWindowVisibility } = useCreateWindow();

// 打开微信主窗口
const handleOpenWechat = async () => {
  // 显示聊天窗口
  await setWindowVisibilityAndTaskbar('chat', true);
  // 隐藏tray窗口
  await setWindowVisibility('tray', false);
};

// 设置
const handleSetting = async () => {
  // 创建设置窗口
  await openWindow('setting', {
    title: '设置',
    width: 550,
    height: 680,
    resizable: false,
    decorations: false,
  });
  // 隐藏tray窗口
  await setWindowVisibility('tray', false);
};

// 退出
const handleExit = async () => {
  try {
    await invoke('app_exit');
  } catch (error) {
    console.error('退出应用失败:', error);
  }
};

/************************  以下处理图标闪烁相关  ************************************/
import { TrayIcon } from '@tauri-apps/api/tray'
import { ref } from 'vue';
import { notificationService } from "@/utils/notification.js";

// 闪烁的定时器
let flashFlagTimer = null
// 图标是否可见
const isIconVisible = ref(true)

// 开始闪烁，如果已经在闪烁了，就不给再闪了
const handleStartFlash = async () => {
  if(flashFlagTimer) {
    await notificationService.send({
      title: '病毒入侵',
      body: '图标已闪烁，请勿重复操作！你要亮瞎我的眼吗？？？'
    })
    return false;
  }
  flashFlagTimer = setInterval(async () => {
    const tray = await TrayIcon.getById('tray');
    // 此处可以自定义图标，此处使用默认图标
    try {
      await tray?.setIcon(isIconVisible.value ? 'icons/128x128.png' : null)
    } catch (e) {
      console.warn('[Tray] 设置托盘图标失败:', e)
    }
    isIconVisible.value = !isIconVisible.value
  }, 500);

  // 隐藏tray窗口
  await setWindowVisibility('tray', false);
};

// 停止闪烁
const handleStopFlash = async () => {
  if(flashFlagTimer) {
    clearInterval(flashFlagTimer)
    flashFlagTimer = null

    // 恢复托盘图标为默认状态，防止图标消失
    try {
      const tray = await TrayIcon.getById('tray')
      await tray?.setIcon('icons/128x128.png')
    } catch (e) {
      console.warn('[Tray] 恢复托盘图标失败:', e)
    }
    isIconVisible.value = true
  }
  // 隐藏tray窗口
  await setWindowVisibility('tray', false);
}

</script>

<style scoped>
.actions-box {
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  padding: 4px 0;
  width: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.custom-button {
  padding: 4px 8px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 14px;
  color: #333;
  text-align: left;
}

.custom-button:hover {
  background-color: #f0f0f0;
}

.custom-button.exit:hover {
  background-color: #ff4d4f;
  color: #fff;
}
</style>
